﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using IndexMarkSilverlightClient;
using System.ComponentModel;
using System.Collections.ObjectModel;
using IndexMarkSilverlightClient.Classes;
using IndexMarkSilverlightClient.IndexMarkServiceReference;
using System.Windows.Browser;

namespace IndexMarkSilverlightClient.Views.Controls.ReportsControls
{
    public partial class USTUTreeView: UserControl, INotifyPropertyChanged
    {
        IndexMarkServiceClient client = null;

        private ObservableCollection<Department> depTree = new ObservableCollection<Department>();

        public ObservableCollection<Department> DepTree
        {
            get 
            {
                return depTree; 
            }
            set
            {
                depTree = value; 
            }
        }

        public event EventHandler LoadCompleted;

        public IEnumerable<Respondent> BlackTeahers
        {
            get 
            {
                List<Respondent> list = new List<Respondent>();
                foreach (Department Dep in depTree)
                {
                    list.AddRange(GetTeahersFromDep(Dep));
                }
                return list.Where(x => x.IsSelected == false).ToList(); 
            }
        }

        public IEnumerable<Respondent> Teahers
        {
            get
            {
                List<Respondent> list = new List<Respondent>();
                foreach (Department Dep in depTree)
                {
                    list.AddRange(GetTeahersFromDep(Dep));
                }
                return list.Where(x => x.IsSelected == true).ToList();
            }
        }

        private int iDTerm;
        
        public int IDTerm 
        {
            get
            {
                return iDTerm;
            }
            set
            {
                iDTerm = value;
                LoadTree();
            }
        }

        public bool Check { get; set; }
    
        public void LoadTree()
        {
            client.GetUSTUTreeAsync(iDTerm, Check);
        }

        private IEnumerable<Respondent> GetTeahersFromDep(Department Dep)
        {
            List<Respondent> list = new List<Respondent>();
            foreach (DTOBase Obj in Dep.Childs)
            {
                if (Obj is Respondent)
                {
                    list.Add(Obj as Respondent);
                }
                if (Obj is Department)
                {
                    list.AddRange(GetTeahersFromDep(Obj as Department));
                }
            }
            return list;
        }
        
        public USTUTreeView()
        {
            InitializeComponent();
            client = new IndexMarkServiceClient();
            client.GetUSTUTreeCompleted += new EventHandler<GetUSTUTreeCompletedEventArgs>(client_GetUSTUTreeCompleted);
        }

        void client_GetUSTUTreeCompleted(object sender, GetUSTUTreeCompletedEventArgs e)
        {
            depTree = e.Result;
            this.DataContext = depTree;

            PropertyChangedInit("DepTree");
            PropertyChangedInit("Teahers");
        }

        void ViewModel_SelectTerm(object sender, Classes.SelectTermEventsArgs e)
        {
            client.GetUSTUTreeAsync(e.SelectTerm.IDTerm);
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            //добавляем параметры в строку запроса
            HtmlDocument doc = HtmlPage.Document;
            String Result = "";
            foreach (Respondent R in Teahers)
            {
                Result += R.IDRespondent + " ";
            }
            doc.QueryString.Add(new KeyValuePair<string,string>("Respondents", Result));
            
            
            if (USTUTree.SelectedItem is Department)
            {
                CheckTree(USTUTree.SelectedItem as Department, (USTUTree.SelectedItem as Department).IsSelected);
            }
            PropertyChangedInit("DepTree");
        }

        private void CheckTree(Department D, bool check)
        {
            foreach (DTOBase obj in D.Childs)
            {
                if (obj is Department)
                {
                    (obj as Department).IsSelected = check;
                    CheckTree(obj as Department, check);
                }
                else
                {
                    (obj as Respondent).IsSelected = check;
                }
            }
        }

        public void UncheckTree()
        {
            foreach (Department D in depTree)
            {
                 D.IsSelected = false;
                 CheckTree(D, false);
            }
        }

        private void PropertyChangedInit(String PropertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
            }
        }

    }
}
